package cn.xyinping.pet.controller;

import cn.xyinping.basic.exception.MyException;
import cn.xyinping.basic.util.AjaxResult;
import cn.xyinping.basic.util.LoginContextUtil;
import cn.xyinping.basic.util.PageList;
import cn.xyinping.pet.domain.Pet;
import cn.xyinping.pet.domain.SearchMasterMsg;
import cn.xyinping.pet.query.SearchMasterMsgQuery;
import cn.xyinping.pet.service.ISearchMasterMsgService;
import cn.xyinping.user.domain.Logininfo;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;


    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return seachMasterMsgService.findAll();
    }

    /**
     * 寻主发布
     * @param smm
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg smm, HttpServletRequest request) {
        try {
            // 获取logininfo
            Logininfo logininfo = LoginContextUtil.getLoginContext(request);
            // 获取user_id
            seachMasterMsgService.publish(smm,logininfo.getId());
            return AjaxResult.me();
        } catch (MyException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统异常");

        }
    }

    /**
     * 未处理
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        try {
            // 获取logininfo
            Logininfo logininfo = LoginContextUtil.getLoginContext(request);
            return seachMasterMsgService.pending(query, logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    /**
     * 已处理
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        try {
            // 获取logininfo
            Logininfo logininfo = LoginContextUtil.getLoginContext(request);
            return seachMasterMsgService.processed(query, logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    /**
     * 未处理数据处理为已处理数据
     * @param pet
     * @param request
     * @return
     */
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet,HttpServletRequest request){
        try {
            // 获取logininfo
            Logininfo logininfo = LoginContextUtil.getLoginContext(request);
            seachMasterMsgService.handle(pet,logininfo.getId());
            return AjaxResult.me();
        } catch (MyException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙");
        }
    }
}
